package com.cyz.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyz.dao.SupplierDao;
import com.cyz.entity.SupplierEntity;
import com.cyz.entity.TypeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 *
 *
 * @author chenyizhen
 * @email sunlightcs@gmail.com
 * @date 2023-12-18 15:02:10
 */
@RestController
@RequestMapping("user/supplier")
public class SupplierController {

    @Autowired
    SupplierDao supplierDao;

    @PostMapping("select")
    public List<String> list(){
        return supplierDao.selectList(new QueryWrapper<>()).stream().map(item-> item.getName()).collect(Collectors.toList());
    }

    @PostMapping("selectOne")
    public SupplierEntity selectByName(String name){
        return supplierDao.selectOne(new QueryWrapper<SupplierEntity>().eq("name", name));
    }

    @PostMapping("list")
    public List<SupplierEntity> supplierEntityList(){
        return supplierDao.selectList(new QueryWrapper<>());
    }

    @PostMapping("insert")
    public String insertSupper(String name, String tel){
        SupplierEntity supplierEntity = new SupplierEntity();
        supplierEntity.setName(name);
        supplierEntity.setTel(tel);
        int insert = supplierDao.insert(supplierEntity);

        return insert == 0 ? "添加失败" : "添加成功";
    }

    @PostMapping("update")
    public String update(SupplierEntity supplierEntity){
        SupplierEntity entity = supplierDao.selectById(supplierEntity.getId());
        if (entity == null){
            return "查询不到";
        }
        int i = supplierDao.updateById(supplierEntity);
        return i == 0 ? "更新失败" : "更新成功";
    }

    @PostMapping("delete/{id}")
    public String delete(@PathVariable int id){
        SupplierEntity supplierEntity = supplierDao.selectById(id);
        if (supplierEntity == null){
            return "空";
        }
        int i = supplierDao.deleteById(id);
        return i == 0 ? "删除失败" : "删除成功";
    }
}
